//业务加载
$(function(){
	//我的角色列表
	function myRoleList() {
		$.ajax({
			type:"post",
			url: ajaxUrl + "/api/1.0/player/role/list.js",
			data:{
				token : window.localStorage.getItem('token'),
				sid: currentSID,
				gameId: 'BOXY',
				jobId : getTimeMs()
			},
			dataType:"json",
			success:function(request){
				console.log(request);	
				showRoleList(request.data);
			},
			error:function(error){
				console.log(error);	
			}
		});		
	}	

	//角色列表展示
	function showRoleList(arr) {
		var str;
		var dom;
		if(!arr.length){
			return;
		}
		
		for(var i = 0 ; i < arr.length ; i++){
			//-------------------------角色信息--------------------------------------
			dom = $('#roletd'+i);
			dom.html('');
			str = '';
			
			str =  str + '									<table>';
			str =  str + '										<tr>';
			str =  str + '											<td align="center">名称</td>';
			str =  str + '											<td colspan="3" class="form-jueseliebiao-role">' + arr[i].roleName + '</td>';
			str =  str + '										</tr>';
			str =  str + '										<tr>';
			str =  str + '											<td style="width: 6%" align="center">职业</td>';
			str =  str + '											<td style="width: 10%" class="form-jueseliebiao-role">' + arr[i].workName + '</td>';
			str =  str + '											<td style="width: 6%" align="center">称号</td>';
			str =  str + '											<td style="width: 10%" class="form-jueseliebiao-role">' + arr[i].workName + '</td>';//titleName
			str =  str + '										</tr>';
			str =  str + '										<tr>';
			str =  str + '											<td align="center">等级</td>';
			str =  str + '											<td class="form-jueseliebiao-role">' + arr[i].level + '</td>';
			str =  str + '											<td align="center">战力</td>';
			str =  str + '											<td class="form-jueseliebiao-role">' + arr[i].combat + '</td>';
			str =  str + '										</tr>';
			str =  str + '									</table>';
			
			console.log(str);
			dom.append(str);
			//--------------------------按钮信息-------------------------------------
			dom = $('#gamebtn'+i);
			dom.html('');
			str = '';
			str =  str + '									<input type="button" value="进入" class="gamebtn" onclick="playgame(\'' + arr[i].id + '\')">';
			
			console.log(str);
			dom.append(str);
		}	
	}

	//获取职业列表
	function gameRoleList() {
		$.ajax({
			type:"post",
			url: ajaxUrl + "/api/1.0/nsgs/system/role.js",
			data:{
				token : window.localStorage.getItem('token'),
				sid: currentSID,
				gameId: 'BOXY',
				jobId : getTimeMs()
			},
			dataType:"json",
			success:function(request){
				console.log(request);	
				showGameRoleList(request.data);
			},
			error:function(error){
				console.log(error);	
			}
		});		
	}	

	//角色列表展示
	function showGameRoleList(arr) {
		var str;
		var dom;
		if(!arr.length){
			return;
		}
		dom = $('#youxijuese');
		dom.html('');
		str = '';
		systemroles = '';
		//-------------------------职业列表--------------------------------------
		for(var i = 0 ; i < arr.length ; i++){
			//职业ID序列
			systemroles = systemroles + arr[i].id + ';';
			//职业展示
			str =  str + '											<td align="center" onclick="youxijuesexuanzhe(\'' + arr[i].id + '\',\'' + arr[i].roleName + '\')">';
			str =  str + '												<table class="form-juesechuangjian-zhiye" style="color: red;">';
			str =  str + '													<tr>';
			str =  str + '														<td style="font-size: 14px;text-align: center;" id="systemrole' + arr[i].id + '">' + arr[i].roleName + '</td>';
			str =  str + '													</tr>';
			str =  str + '													<tr>';
			str =  str + '														<td style="font-size: 14px;text-align: center;"><img src="' + arr[i].rolePicUrl + '" style="width: 60px;height: 68px;"></td>';
			str =  str + '													</tr>';
			str =  str + '												</table>';
			str =  str + '											</td>';
		}	
		console.log(str);
		dom.append(str);
		
		youxijuesexuanzhe(arr[0].id,arr[0].roleName);
	}



	//第一次加载
	myRoleList();
	gameRoleList();
	xingbiexuanzhe(1);
});

//------------------------------------以下是进入游戏------------------------------------------------
function playgame(id) {
	window.localStorage.setItem('player_role_id', id);
	top.location.href="/boxy/game.html";
}

function newplayer() {
	//获得昵称
	var roleName = $('#roleName').val();
	if(checkEmpty(roleName)){//判断roleName是否有效
		console.log("游戏角色昵称为空！");
		return;
	}
	//请求创建用户
	$.ajax({
		type:"post",
		url: ajaxUrl + "/api/1.0/player/role/new.js",
		data:{
			token : window.localStorage.getItem('token'),
			gameId: 'BOXY',
			jobId : getTimeMs(),
			roleName: roleName,
			workId: workId,
			workName: workName,
			roleSex: roleSex
		},
		dataType:"json",
		success:function(request){
			console.log(request);	
			showRoleList(request.data);
		},
		error:function(error){
			console.log(error);	
		}
	});	
	//开始游戏
	playgame('123');
	
}

//-------------------------------------------------以下是页面交互-------------------------------------------------------------
var roleSex = '1';
function xingbiexuanzhe(type){
	roleSex = type;
	if( type==1 ){
		$("#xingbiexuanzhe1").css("background","");
		$("#xingbiexuanzhe1").css("color","#e7220c"); //266dd9
		$("#xingbiexuanzhe2").css("background","");
		$("#xingbiexuanzhe2").css("color","");
	}else{
		$("#xingbiexuanzhe1").css("background","");
		$("#xingbiexuanzhe1").css("color","");
		$("#xingbiexuanzhe2").css("background","");
		$("#xingbiexuanzhe2").css("color","#e7220c");
	}
}

var systemroles;
var workId;
var workName;
function youxijuesexuanzhe(id,name){
	var arr = systemroles.split(';');
	//颜色还原
	for(var i = 0 ; i < arr.length ; i++){
		$("#systemrole"+arr[i]).css("background","");
		$("#systemrole"+arr[i]).css("color",""); //266dd9
	}

	//颜色设置
	workId = id;
	workName = name;
	$("#systemrole"+workId).css("background","");
	$("#systemrole"+workId).css("color","#e7220c");
}
